import Vue from 'vue'
import VueRouter from 'vue-router'
import Login from '../components/Login.vue'
import ElementUI from '../components/ElementUI.vue'
import Welcome from '../components/Welcome.vue'
import Home from '../components/Home.vue'
import User from '../components/user/User.vue'
import Item from '../components/items/Item.vue'
import ItemCat from '../components/items/ItemCat.vue'
import AddItem from '../components/items/addItem.vue'
//使用路由机制
Vue.use(VueRouter)
const routes = [
  {path: '/', redirect: '/login'},
  {path: '/login', component: Login},
  {path: '/elementUI', component: ElementUI},
  {path: '/home', component: Home,redirect: 'welcome',children:[
    {path: '/welcome', component: Welcome},
    {path: '/user', component: User},
    {path: '/itemCat', component: ItemCat},
    {path: '/item', component: Item},
    {path: '/item/addItem', component: AddItem}
  ]},

]

const router = new VueRouter({
  routes
})
//路由导航守卫!!!!!!!
router.beforeEach((to,from,next) => {
  if(to.path === '/login') return next()

  let token = window.sessionStorage.getItem('token')
  console.log(token)
  if(token === null || token === ''){
    return next("/login")
  }
  next()
})


export default router
